<?php


namespace app\common\model;


use think\model\concern\SoftDelete;
use think\Db;

class ShopsManager extends Base
{
    // 软删除
    use SoftDelete;
    protected $deleteTime = 'is_del';
    protected $defaultSoftDelete = 1;

    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_shops_manager';
    // 主键ID
    protected $pk = 'manager_id';



    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ["{$this->pk}" => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 更新数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->{$this->pk};
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

    // 获取分页列表
    function get_list_page($field,$cond, $orderby ='update_time desc',$limit)
    {
        $where = null;

        if(!empty($cond['shop_id'])){
            $where[] = ['m.shop_id','=', $cond['shop_id']];
        }
        if(!empty($cond['real_name'])){
            $where[] = ['real_name','LIKE', "%{$cond['real_name']}%"];
        }

        $rs = $this->field($field)
            ->alias('m')
            ->leftJoin('ddt_shops s','m.shop_id = s.shop_id')
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }

    // 查询列表
    function get_list($cond, $orderby, $item, $limit)
    {
        // 条件
        $where = null;
        if(!empty($cond['user_name'])){
            $where[] = ['a.user_name','like',"%".$cond['user_name']."%"];
        }

        if(!empty($cond['group_id'])){
            $where[] = ['a.group_id','=',$cond['group_id']];
        }
        if(!empty($cond['not_ins'])){
            $not_in = is_array($cond['not_ins'])?implode(',',$cond['not_ins']):$cond['group_id'];
            $where[] = ['a.group_id','not in',$not_in];
        }


        $rs = Db::table($this->table)
            ->alias('a')
            ->leftJoin('ddt_group b','a.group_id = b.group_id')
            ->where($where)
            ->field($item)
            ->order($orderby)
            ->paginate($limit);
        return $rs;
    }


    function Del($id)
    {
        $article = ShopsManager::get($id);
//		var_dump($article);
        return $article->delete();
    }


}